<?php
include('header.php');
if(!$is_online){
	redirect('index.php');
	exit;
}

if(isset($_POST['submit'])) {
	$code = $db->EscapeString($_POST['code']);
	
	$sql = $db->Query("SELECT * FROM `coupons` WHERE `code`='".$code."' AND `used`='0'");
	$ext = $db->FetchArray($sql);
	if($ext['id'] != ""){
		$db->Query("UPDATE `users` SET `coins`=`coins`+'".$ext['coins']."' WHERE `id`='".$data['id']."'");
		$db->Query("UPDATE `coupons` SET `used`='1' WHERE `code`='".$code."'");
		$mesaj = '<div class="msg"><div class="msg success"><b>Success!</b> You have received <b>'.$ext['coins'].' coins</b>!</div></div>';
	}else{
		$mesaj = '<div class="msg"><div class="error"><b>ERROR:</b> This coupon code doesn\'t exist or is already used!</div></div>';
	}
}?>
<div class="block medium right">
			<div class="top">		
					<h1>Redeem Coupon</h1>
			</div>
			
		<div class="content"><?=$mesaj?> 
		

<form method="post">
<table class="form">
	<tr>
		<td width="150px"><b>Coupon Code</b></td>
		<td><input type="text" name="code" /><br /></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="submit" value="Submit" /></td>
	</tr>
</table>
</form>
				</div>
			</div>		
<?include('footer.php');?>